package br.com.jk.consultaxml;

/**
 *
 * @author camilo
 */
public class BuscaXmlJDialog extends javax.swing.JDialog {

  private static BuscaXmlJDialog dialog;
  BuscaXmlTableModel prodTm = new BuscaXmlTableModel();

  private static void setDados(BuscaXml xml) {
    dialog.buscaXml = xml;
    dialog.jtfCnpjDest.setText(xml.getDestinatarioCnpj());
    dialog.jtfCnpjForn.setText(xml.getFornecedorCnpj());
    dialog.jtfRazaoDest.setText(xml.getDestinatarioRazao());
    dialog.jtfRazaoForn.setText(xml.getFornecedorRazao());
    String totalNfe = "R$ " + xml.getTotalNfe().toString();
    dialog.jtfTotalNfe.setText(totalNfe);
    dialog.jtfNumNf.setText(xml.getNumNfe().toString());
    dialog.setTitle(xml.getArquivo().getName());
    
    dialog.prodTm.adicionaLista(xml.getProdutos());
  }
  private BuscaXml buscaXml;

  //Singleton
  public static BuscaXmlJDialog getInstance(java.awt.Frame parent, boolean modal, BuscaXml xml) {
    if (dialog == null) {
      dialog = new BuscaXmlJDialog(parent, modal, xml);
    }
    setDados(xml);
    return dialog;
  }

  /**
   * Creates new form BuscaXmlJDialog
   */
  private BuscaXmlJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    setLocationRelativeTo(null);
    criaModel();
  }

  private BuscaXmlJDialog(java.awt.Frame parent, boolean modal, BuscaXml xml) {
    super(parent, modal);
    initComponents();
    setLocationRelativeTo(null);
    criaModel();
  }

  private void criaModel() {
    jtProduto.setModel(prodTm);

    prodTm.sorter(jtProduto, prodTm);
    
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_CFOP).setPreferredWidth(55);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_COD).setPreferredWidth(90);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_DESCRICAO).setPreferredWidth(300);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_EMBALAGEM).setPreferredWidth(40);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_ITEM).setPreferredWidth(30);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_NCM).setPreferredWidth(80);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_QUANTIDADE).setPreferredWidth(60);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_VALOR).setPreferredWidth(80);
    jtProduto.getColumnModel().getColumn(BuscaXmlTableModel.COLUNA_COD_VALOR_TOTAL).setPreferredWidth(80);
  }
  
  private void limpar(){
    while (prodTm.getRowCount()>0) {
      prodTm.remove(0);
    }
    jtfCnpjDest.setText(null);
    jtfCnpjForn.setText(null);
    jtfRazaoDest.setText(null);
    jtfRazaoForn.setText(null);
    jtfTotalNfe.setText(null);
    jtfNumNf.setText(null);
    dialog.setTitle("Nova consulta");
  }
  
  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
   * code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jtfCnpjForn = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jtfRazaoForn = new javax.swing.JTextField();
    jLabel7 = new javax.swing.JLabel();
    jtfNumNf = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jtfTotalNfe = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    jtfCnpjDest = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    jtfRazaoDest = new javax.swing.JTextField();
    jbNovaConsulta = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jtProduto = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Arquivo Xml"));
    jPanel1.setLayout(new java.awt.GridLayout(14, 0, 0, 5));

    jLabel2.setText("CNPJ Forn.:");
    jPanel1.add(jLabel2);

    jtfCnpjForn.setEditable(false);
    jtfCnpjForn.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfCnpjForn.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPanel1.add(jtfCnpjForn);

    jLabel3.setText("Razão Social Fornecedor:");
    jPanel1.add(jLabel3);

    jtfRazaoForn.setEditable(false);
    jtfRazaoForn.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfRazaoForn.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPanel1.add(jtfRazaoForn);

    jLabel7.setText("Nº Nf-e");
    jPanel1.add(jLabel7);

    jtfNumNf.setEditable(false);
    jtfNumNf.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfNumNf.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jtfNumNf.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jtfNumNfActionPerformed(evt);
      }
    });
    jPanel1.add(jtfNumNf);

    jLabel4.setText("Total da NF-e");
    jPanel1.add(jLabel4);

    jtfTotalNfe.setEditable(false);
    jtfTotalNfe.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfTotalNfe.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPanel1.add(jtfTotalNfe);

    jLabel5.setText("CNPJ Destinatário:");
    jPanel1.add(jLabel5);

    jtfCnpjDest.setEditable(false);
    jtfCnpjDest.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfCnpjDest.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPanel1.add(jtfCnpjDest);

    jLabel6.setText("Razão Social Destinatário:");
    jPanel1.add(jLabel6);

    jtfRazaoDest.setEditable(false);
    jtfRazaoDest.setFont(new java.awt.Font("Liberation Sans", 0, 14)); // NOI18N
    jtfRazaoDest.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jPanel1.add(jtfRazaoDest);

    jbNovaConsulta.setText("Nova Consulta");
    jbNovaConsulta.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbNovaConsultaActionPerformed(evt);
      }
    });
    jPanel1.add(jbNovaConsulta);

    jtProduto.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    jScrollPane1.setViewportView(jtProduto);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(jScrollPane1))
        .addGap(16, 16, 16))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void jtfNumNfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfNumNfActionPerformed
    // TODO add your handling code here:
  }//GEN-LAST:event_jtfNumNfActionPerformed

  private void jbNovaConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbNovaConsultaActionPerformed
    limpar();
    ProcessaXmlUtil.main();
  }//GEN-LAST:event_jbNovaConsultaActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(BuscaXmlJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(BuscaXmlJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(BuscaXmlJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(BuscaXmlJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        BuscaXmlJDialog dialog = new BuscaXmlJDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JButton jbNovaConsulta;
  private javax.swing.JTable jtProduto;
  private javax.swing.JTextField jtfCnpjDest;
  private javax.swing.JTextField jtfCnpjForn;
  private javax.swing.JTextField jtfNumNf;
  private javax.swing.JTextField jtfRazaoDest;
  private javax.swing.JTextField jtfRazaoForn;
  private javax.swing.JTextField jtfTotalNfe;
  // End of variables declaration//GEN-END:variables
}
